// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/**
 * @brief
 *
 * @version  0.1
 * @author   yuanchenxi
 * @date     2024.12.20
 */

#include "PWELDParser.h"

#include <algorithm>
#include <cstdint>

#include "boost/algorithm/string.hpp"

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Property/IPWELDService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (PWELD);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/PWELD/PWELD.md>
*/
// 以下需要修改
enum PWELDkey : std::uint8_t
{
    PWELDKEY_PID   = 2,
    PWELDKEY_MID   = 3,
    PWELDKEY_D     = 4,
    PWELDKEY_MSET  = 7,
    PWELDKEY_TYPE  = 9,
    PWELDKEY_LDMIN = 12,
    PWELDKEY_LDMAX = 13,
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParsePWELD (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    // 初始化临时的PWELD数据结构，用于存储解析后的数据
    SG::DataStructure::FEM::PWELDData tempPWELD;
    tempPWELD.m_MINLEN   = 0.2;
    tempPWELD.m_MAXLEN   = 5.0;
    tempPWELD.m_WELDTYPE = -1;  // general connector:-1
    // TODO(bug 001) m_domainId的正确填写，目前默认设置为1
    tempPWELD.m_domainId = 1;

    int curIDKey = 1;  // 当前处理的字段索引

    // 遍历分割后的字符串条目
    for (auto curStrKey : dividedEntryInfo)
    {
        // 检查当前字符串是否为空（只包含空格）
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;  // 跳过空字段，继续下一个字段
            continue;
        }

        // 根据当前字段索引解析相应的数据并存储到tempPWELD
        switch (curIDKey)
        {
        case PWELDKEY_PID:
            tempPWELD.m_id = convertToI64 (curStrKey);
            break;
        case PWELDKEY_MID:
            tempPWELD.m_MID = convertToI64 (curStrKey);
            break;
        case PWELDKEY_D:
            tempPWELD.m_D = convertToReal (curStrKey);
        case PWELDKEY_MSET:
            boost::trim (curStrKey);
            if (curStrKey[1] == 'n' || curStrKey[1] == 'N')
            {
                tempPWELD.m_EXTCON = 1;
            }
            break;
        case PWELDKEY_TYPE:
            // block不会走到这，默认为-1
            tempPWELD.m_WELDTYPE = 0;
            break;
        case PWELDKEY_LDMIN:
            tempPWELD.m_MINLEN = convertToReal (curStrKey);
            break;
        case PWELDKEY_LDMAX:
            // 解析坐标系CR1，将其转换为实型并存储到m_CR
            tempPWELD.m_MAXLEN = convertToReal (curStrKey);
            break;
        default:
            // 如果索引不匹配任何已知字段，跳过
            break;
        }

        curIDKey++;  // 处理完当前字段，移动到下一个字段
    }

    // 将解析后的PWELD数据添加到数据库服务中
    return dbServiceFactory->get<IPWELDService> ()->Add (tempPWELD);
}